package com.zhongyu.controller;

import com.zhongyu.pojo.OrderDetail;
import com.zhongyu.pojo.XsView;
import com.zhongyu.service.XSreportService;
import com.zhongyu.vo.ResultVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cus/xsqsfx")
public class XsReport1Controller {
    @Resource
    private XSreportService xSreportService;


    @GetMapping("/list")
    public ResultVo orderReport1(int year) {
        List<XsView>list = xSreportService.findAllbyYear(year);
        List<XsView> list1=new ArrayList<>();
        for(XsView orderDetail:list){
            for(int i=0;i<12;i++){
                int a=i+1;
                if(!orderDetail.getMonth().equals(String.valueOf(a))){
                    XsView detail=new XsView();
                    detail.setMonth(String.valueOf(a));
                    detail.setMoney((double) 0);
                    list1.add(new XsView(detail.getMonth(), detail.getMoney()));
                }
                if(orderDetail.getMonth().equals(String.valueOf(a))){
                    String month = orderDetail.getMonth();
                    Double totalMoney = orderDetail.getMoney();
                    list1.add(new XsView(month, totalMoney));
                }
            }
        }
        return ResultVo.success(list1);
    }

    @GetMapping("/EcharsShow")
    public List<XsView> orderReport2(String year) {
        List<XsView>list = xSreportService.findAllbyYear(Integer.parseInt(year));
        List<XsView> list1=new ArrayList<>();
        for(XsView orderDetail:list){
            for(int i=0;i<12;i++){
                int a=i+1;
                if(!orderDetail.getMonth().equals(String.valueOf(a))){
                    XsView detail=new XsView();
                    detail.setMonth(String.valueOf(a));
                    detail.setMoney((double) 0);
                    list1.add(new XsView(detail.getMonth(), detail.getMoney()));
                }
                if(orderDetail.getMonth().equals(String.valueOf(a))){
                    String month = orderDetail.getMonth();
                    Double totalMoney = orderDetail.getMoney();
                    list1.add(new XsView(month, totalMoney));
                }
            }
        }
     /*   for (OrderDetail detail :list){
            if (!detail.getMonth().equals(1)){
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setMonth("1");
                orderDetail.setTotalmoney((double) 0);
                list1.add(new XsView(orderDetail.getMonth(), orderDetail.getTotalmoney()));
            }
            if (!detail.getMonth().equals(2)){
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setMonth("2");
                orderDetail.setTotalmoney((double) 0);
                list1.add(new XsView(orderDetail.getMonth(), orderDetail.getTotalmoney()));
            }
            if (!detail.getMonth().equals(3)){
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setMonth("3");
                orderDetail.setTotalmoney((double) 0);
                list1.add(new XsView(orderDetail.getMonth(), orderDetail.getTotalmoney()));            }
            if (!detail.getMonth().equals(4)){
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setMonth("4");
                orderDetail.setTotalmoney((double) 0);
                list1.add(new XsView(orderDetail.getMonth(), orderDetail.getTotalmoney()));            }
            if (!detail.getMonth().equals(5)){
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setMonth("5");
                orderDetail.setTotalmoney((double) 0);
                list1.add(new XsView(orderDetail.getMonth(), orderDetail.getTotalmoney()));            }
            if (!detail.getMonth().equals(6)){
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setMonth("6");
                orderDetail.setTotalmoney((double) 0);
                list1.add(new XsView(orderDetail.getMonth(), orderDetail.getTotalmoney()));            }
            String month = detail.getMonth();
            Double totalMoney = detail.getTotalmoney();
            list1.add(new XsView(month, totalMoney));
            if (!detail.getMonth().equals(7)){
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setMonth("7");
                orderDetail.setTotalmoney((double) 0);
                list1.add(new XsView(orderDetail.getMonth(), orderDetail.getTotalmoney()));            }
            if (!detail.getMonth().equals(8)){
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setMonth("8");
                orderDetail.setTotalmoney((double) 0);
                list1.add(new XsView(orderDetail.getMonth(), orderDetail.getTotalmoney()));            }
            if (!detail.getMonth().equals(9)){
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setMonth("9");
                orderDetail.setTotalmoney((double) 0);
                list1.add(new XsView(orderDetail.getMonth(), orderDetail.getTotalmoney()));            }
            if (!detail.getMonth().equals(10)){
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setMonth("10");
                orderDetail.setTotalmoney((double) 0);
                list1.add(new XsView(orderDetail.getMonth(), orderDetail.getTotalmoney()));            }
            if (!detail.getMonth().equals(11)){
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setMonth("11");
                orderDetail.setTotalmoney((double) 0);
                list1.add(new XsView(orderDetail.getMonth(), orderDetail.getTotalmoney()));            }
            if (!detail.getMonth().equals(12)){
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setMonth("12");
                orderDetail.setTotalmoney((double) 0);
                list1.add(new XsView(orderDetail.getMonth(), orderDetail.getTotalmoney()));            }
        }*/
       return  list1;
    }

  /* public List<Map<String, Object>> trendReport() {
        //保存每个月份的销售额
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(12);
        //获取当年每月销售额
        List<Map<String,Object>> yearData = reportDao.getSumMoney(year);
        //把从DB中得到的当年每月销售额转换成map集合，用于下面的查缺补漏，因为可能存在某些月份没有销售额的情况
        Map<String, Map<String, Object>> map = new HashMap<String, Map<String, Object>>();
        for (Map<String, Object> m : yearData) {
            map.put((String) m.get("month"), m);
        }

        Map<String, Object> data = null;
        //按12个月，对每个月份的数据进行封装，最终以List<Map<String,Object>>形式返回
        for (int i = 1; i <= 12; i++) {
            data = map.get(i + "月");
            if (null == data) {
                //如果当月没有销售额，则补上当月的月份和数据0
                data = new HashMap<String, Object>();
                data.put("month", i + "月");
                data.put("money", 0);
            }
            result.add(data);
        }
        return result;
    }*/
}
